package com.cqut.forum.dao.imp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.cqut.forum.dao.PictureReplyDao;

/**
 * 图片评论dao实现类
 * @author yangxun
 */
@Component
public class PictureReplyDaoImp extends BaseDaoImp implements PictureReplyDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getPictureReplys(List<Integer> pictureIds) {
		String sql = "select u.user_id,u.user_photo,u.user_name,r.review_contents," +
				"r.review_time,r.picture_id,r.review_id from user u,review r " +
				"where r.review_user_id = u.user_id and r.picture_id in (";

		List<Object> parameters = new ArrayList<Object>();
		for(Integer i : pictureIds){
			sql = sql + "?,";
			parameters.add(i);
		}
		sql = sql.substring(0,sql.length()-1)+") order by r.review_time";
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql, parameters, null);
		
		return result;
	}

	@Override
	public boolean changeReplyState(int userId) {
		String sql = "update review r,picture p set r.review_state=false where r.picture_id=p.picture_id and p.owner_id=?";
		Query query = super.getSqlQuery(sql);
		query.setInteger(0, userId);
		query.executeUpdate();
		return true;
	}

	
}
